9/26/2020

Interactive plots are tricky

  • An interactive plot is any plot that is reactive to use input;
  • It can be difficult to both develop and share interactive plots, as extensive libraries are needed for both of these parts
  • There are a couple of packages for making interactive plots in R. While these packages make it relatively easy to make interactive plots, sharing them can be a little more tricky, and We’ll cover it a little more in future classes

ggiraph

  • A simple and lightweight interactive plotting library is ggiraph. It use ggplot style syntax, and provides some drop in replacement for some ggplot geoms
library(ggplot2)
library(ggiraph)
data <- mtcars
data$carname <- row.names(data)
gg_point = ggplot(data = data) +
    geom_point_interactive(aes(x = wt, y = qsec, color = disp,
    tooltip = carname, data_id = carname)) + 
     theme_minimal()
girafe(ggobj = gg_point)
  • ggiraph is limited to adding tooltips ( info displayed on hover), and can’t really do much beyond that

Plotly

  • Plotly is a very powerful library for making interactive plots. Its a little weird in that plotly is developed by a independent company, but makes their library open source. If you use plotly, they do add a small watermark to your plots

-Most of plotly is actually a javascript library, and the functions in R are wrapper on top of the java script code.

  • but its worth it, because you can create some pretty cool plots
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
mtcars$am <- as.factor(mtcars$am)

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~am, colors = c('#BF382A', '#0C4B8E'))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
                     yaxis = list(title = 'Gross horsepower'),
                     zaxis = list(title = '1/4 mile time')))

fig
  • plotly has a ggplot-esque API (thank you yigit Erol for the example)
library(plotly)
x <- c(2015:2020)
Beverages <- rnorm(x, mean = 10)
Grocery <- rnorm(x, mean = 1)
Snack <- rnorm(x, mean = 5)
x <- c(2015:2020)
df <- data.frame(x, Beverages, Grocery, Snack)
df
##      x Beverages     Grocery    Snack
## 1 2015 10.469239  1.45719000 5.311718
## 2 2016  9.515297  1.28417643 3.522927
## 3 2017  9.822441  3.22304287 4.388352
## 4 2018 10.895643 -0.50802840 5.265209
## 5 2019 10.099394  0.55360442 5.154883
## 6 2020  8.984024 -0.06715201 4.686314
  • now the actual plot. plot_ly is very loose with how it populates data, so what ever parameters we don’t specify, it will try and guess from the data we provide
fig <- plot_ly(data = df, x = ~x, marker=list(size=10))
fig
## No trace type specified:
##   Based on info supplied, a 'histogram' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#histogram
  • we didn’t provide a mapping what type of plot we wanted, and so it defaults to a bar graph
 plot_ly(data = df, x = ~x,type = 'scatter', marker=list(size=10))
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

-we told it scatter plot, but didn’t provide any Y data

plot_ly(data = df, x = ~x,tmarker=list(size=10)) %>% 
    add_trace(y = ~Beverages, name = 'Beverage',mode = 'lines') %>% 
    add_trace(y = ~Grocery, name = 'Grocery', mode = 'lines+markers') %>% 
    add_trace(y = ~Snack, name = 'Snack', mode = 'markers') %>% 
    layout(title = 'Sales by Catergories Between 2015 & 2020', 
                      yaxis = list(title = 'Average Sales'),
                      xaxis = list(title = 'Time') ) 
  • note that we build graphs iteratively, unlike ggplot where all plots of a certain type are specified at once

  • plotly does provide function to auto convert ggplots into plotly plots

df_long <- df %>% pivot_longer(-x)
gg <- ggplot(df_long) + 
    geom_line(aes(x=x, y=value, color = name))
gg

gg %>% ggplotly()
  • This only works for simple plots; the 3D plot above couldn’t be created like this, because ggplot only understands x and y aesthetics

  • Between these two libraries, you should have enough to start making your own plots